#include "nrf_port.h"
#include "hal_nrf.h"
#include "gzll.h"


// ==================================================================
/// @brief  : 向NRF写入一个值
/// @param  : value 要写入的数据
/// @returns: uint8_t 返回写入的状态
// ==================================================================
uint8_t port_nrf_write(uint8_t value)
{
    uint8_t temp;

    SPI_I2S_SendData(SPI2, value);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);  //等待发送缓冲区为空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接收缓冲区非空
    temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据

    return 1;
}

// ==================================================================
/// @brief  : 向NRF读一个寄存器的值
/// @param  : reg 要读的寄存器
/// @returns: uint8_t 返回读到的数据
// ==================================================================
uint8_t port_nrf_read_reg(uint8_t reg)
{
    uint8_t temp;
    CSN_LOW();

    SPI_I2S_SendData(SPI2, reg);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);  //等待发送缓冲区为空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接收缓冲区非空
    temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据

    SPI_I2S_SendData(SPI2, 0);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);  //等待发送缓冲区为空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接收缓冲区非空

    temp = SPI_I2S_ReceiveData(SPI2);

    CSN_HIGH();
    return temp;
}

// ==================================================================
/// @brief  : 改写NRF寄存器的值
/// @param  : reg 要改写的寄存器
/// @param  : value 要改写的数据
// ==================================================================
void port_nrf_write_reg(uint8_t reg, uint8_t value)
{
    uint8_t temp;
    CSN_LOW();

    SPI_I2S_SendData(SPI2, W_REGISTER + reg);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接受缓冲器非空
    temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据

    SPI_I2S_SendData(SPI2, value);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接受缓冲器非空
    temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据

    CSN_HIGH();
}

// ==================================================================
/// @brief  : 读多个NRF寄存器的值
/// @param  : reg 要读的寄存器
/// @param  : pbuf 缓存
/// @returns: uint16_t 寄存器+长度
// ==================================================================
uint16_t port_nrf_read_multibyte_reg(uint8_t reg, uint8_t *pbuf)
{
    uint8_t length;
    switch (reg) {
        case HAL_NRF_PIPE0:
        case HAL_NRF_PIPE1:
        case HAL_NRF_TX:
            length = hal_nrf_get_address_width();
            CSN_LOW();
            port_nrf_write(RX_ADDR_P0 + reg);
            break;
        case HAL_NRF_RX_PLOAD: {
            reg = hal_nrf_get_rx_data_source();
            if (7 > reg) {  // 大于7数据错误
                length = hal_nrf_read_rx_payload_width();
                CSN_LOW();
                port_nrf_write(R_RX_PAYLOAD);
            } else {
                length = 0U;
            }
            break;
        }
        default:
            length = 0U;
            break;
    }

    for (uint8_t i = 0; i < length; i ++) {
        SPI_I2S_SendData(SPI2, 0);
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器空
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接受缓冲器非空
        *pbuf++ = SPI_I2S_ReceiveData(SPI2);
    }

    CSN_HIGH();
    return (((uint16_t) reg << 8) | length);
}

// ==================================================================
/// @brief  : 写多个个NRF寄存器的值
/// @param  : reg 要写的寄存器
/// @param  : pbuf 要写的数据
/// @param  : length 数据长度
// ==================================================================
void port_nrf_write_multibyte_reg(uint8_t reg, const uint8_t *pbuf, uint8_t length)
{
    uint8_t temp;
    CSN_LOW();

    SPI_I2S_SendData(SPI2, reg);
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器空
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接受缓冲器非空
    temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据

    for (uint8_t i = 0; i < length; i++) {
        SPI_I2S_SendData(SPI2, *pbuf++);
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器空
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接受缓冲器非空
        temp = SPI_I2S_ReceiveData(SPI2); // 读取无用数据
    }

    CSN_HIGH();
}

void TIM2_IRQHandler(void)   //TIM2中断  1ms
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        gzll_timer_isr_function();
    }
}

// ==================================================================
/// @brief  : gzll协议必须要占用一个定时器用来跳频，用户必须实现
/// @param  : period 周期基准为us
// ==================================================================
void port_set_timer_period(uint16_t period)
{
    TIM2_Configuration(period);
}

static void Flash_Read(uint32_t addr, uint32_t *buf, uint8_t size)
{
    uint8_t *buf_8 = (uint8_t *)buf;
    uint8_t i;

    /*copy from flash to ram */
    for (i = 0; i < size; i++, addr ++, buf_8++) {
        *buf_8 = *(volatile uint8_t *) addr;
    }
}

// ==================================================================
/// @brief  : 获取主机的地址，传给设备使用，用户必须实现
/// @param  : buf 缓存
/// @param  : len 长度一般为5个字节
// ==================================================================
void port_host_chip_id_read(uint8_t *buf, uint8_t len)
{
    #define CHIP_ID_ADDR            0x08000000 + (1024 * 250)

    #if 1
    uint8_t i = 0;
    for (i = 0; i < len; i ++) {
        *buf++ = i+2;
    }
    #else
    Flash_Read(CHIP_ID_ADDR, (uint32_t*)buf, 5);    // 地址通过烧录器滚码写入
    #endif
}

void port_feed_wdt(void)
{
#if 0
    // Wdt_Feed();
#else
    // Pca_FeedWdt();
    IWDG_ReloadCounter();
#endif
}


void port_delay_rx_periods(uint16_t rx_periods)
{
    // port_feed_wdt();
    //delay10us(rx_periods/10);
    while (rx_periods--) {
        for (uint8_t i = 0; i < 50; i++);
    }
}
